﻿


var ajax = function() {
    /* Create a new XMLHttpRequest object to talk to the Web server */
    function CreateHttpRequest() {

        var xmlHttp = false;
        /*@cc_on@*/
        /*@if (@_jscript_version >= 5)
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                xmlHttp = false;
            }
        }
        @end@*/
        if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
            xmlHttp = new XMLHttpRequest();
        }
        return xmlHttp;
    }

    function exception(sender, msg) {
        return { object: sender, msg: msg };
    }

    return {
        post: function(url, data, callback, errorCallback, contentType) {
            var request = CreateHttpRequest();
            if (request) {
                var dataStr = String.toSerialize(data); 
                request.open("POST", url, true);
                request.setRequestHeader('Content-Type',  contentType|| 'application/x-www-form-urlencoded')
                request.setRequestHeader("Cache-Control", "no-cache")
                request.onreadystatechange = function() {
                    if (request.readyState == 4) {
                        try {
                            switch (request.status) {
                                case 200:
                                    callback(request.responseXML, request.responseText);
                                    break;
                                default:
                                    if (errorCallback) errorCallback(new exception("Server Error", request.statusText));
                            }
                        }
                        finally {
                            request = null;
                        }
                    }
                }
                request.send(dataStr);
            }
            return request;
        }
    };
} ();